package com.manage.competition.service.impl;

import com.manage.competition.entity.ExaminationColumn;
import com.manage.competition.mapper.ExaminationColumnMapper;
import com.manage.competition.repository.ExaminationColumnRepository;
import com.manage.competition.service.IExaminationColumnService;
import com.manage.competition.util.CurrentUserUtil;
import com.manage.competition.util.SnowflakeUtil;
import com.manage.competition.vo.ExaminationColumnVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Create with IDEA
 *
 * @Author: gitee.com/KamisamaXX
 * @Date: Create in 16:08 2019/4/2
 * @Description:
 */
@Service
public class ExaminationColumnServiceImpl implements IExaminationColumnService {
    @Autowired
    private ExaminationColumnRepository examinationColumnRepository;

    @Autowired
    private ExaminationColumnMapper examinationColumnMapper;

    @Override
    public List<ExaminationColumnVo> findByExamId(long id) {
        List<ExaminationColumn> list = examinationColumnRepository.findAllByPaperId(id);
        List<ExaminationColumnVo> voList = new ArrayList<>();
        for(ExaminationColumn item : list){
            ExaminationColumnVo vo = new ExaminationColumnVo();
            BeanUtils.copyProperties(item,vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public ExaminationColumnVo findByIdAndPaperId(long id,long paperId) {
        ExaminationColumn examinationColumn = examinationColumnRepository.findByIdAndPaperId(id,paperId);
        if (examinationColumn!=null){
            ExaminationColumnVo vo = new ExaminationColumnVo();
            BeanUtils.copyProperties(examinationColumn,vo);
            return vo;
        }
        return null;
    }

    @Override
    public ExaminationColumn findById(long id) {
        return examinationColumnRepository.findById(id).orElse(null);
    }

    @Override
    public int insert(ExaminationColumn column) {
        column.setCreateUser(CurrentUserUtil.getCurrentUser().getUsername());
        column.setUpdateUser(CurrentUserUtil.getCurrentUser().getUsername());
        return examinationColumnMapper.insert(column);
    }

    @Override
    public int update(ExaminationColumn column) {
        column.setUpdateUser(CurrentUserUtil.getCurrentUser().getUsername());
        return examinationColumnMapper.updateByPrimaryKeySelective(column);
    }

    @Override
    public int delete(ExaminationColumn column) {
        return examinationColumnMapper.deleteByPrimaryKey(column.getId());
    }
}
